/**
 * May 12, 2014 11:16:32 AM
 */
package elitech.vietnam.copyweatherfashion.adapters;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.entities.Post;
import elitech.vietnam.copyweatherfashion.entities.Product;
import elitech.vietnam.copyweatherfashion.utilities.Utilities;

/**
 * @author Cong
 *
 */
public class ShopContentAdapter extends ArrayAdapter<Object> {
	
	boolean mShop = false;

	public ShopContentAdapter(Context context, int resource, List<Object> objects) {
		super(context, resource, objects);
		if (objects.size() > 0 && objects.get(0) instanceof Post)
			mShop = true;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder;
		
		if (row == null) {
			row = LayoutInflater.from(getContext()).inflate(R.layout.layout_shopitem, parent, false);
			
			holder = new ViewHolder();
			holder.mImage = (ImageView) row.findViewById(R.id.shopitem_imgProduct);
			holder.mTxtName = (TextView) row.findViewById(R.id.shopitem_txtName);
			holder.mTxtPrice = (TextView) row.findViewById(R.id.shopitem_txtPrice);
			
			holder.mTxtName.setPaintFlags(holder.mTxtName.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
			holder.mTxtPrice.setPaintFlags(holder.mTxtPrice.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
			
			row.setTag(holder);
		} else 
			holder = (ViewHolder) row.getTag();
		
		final Object mItem = getItem(position);
		if (mItem instanceof Post) {
			Post mPost = (Post) mItem;
			UrlImageViewHelper.setUrlDrawable(holder.mImage, Const.SERVER_IMAGE_URL + mPost.Image);
			holder.mTxtName.setText(mPost.Title);
			holder.mTxtPrice.setText(mPost.Price + Utilities.getPriceCode());
		} else {
			Product mProduct = (Product) getItem(position);
			UrlImageViewHelper.setUrlDrawable(holder.mImage, Const.SERVER_IMAGE_URL + mProduct.Image);
			holder.mTxtName.setText(mProduct.Title);
			holder.mTxtPrice.setText(((int) mProduct.Price) + Utilities.getPriceCode());
		}
		
		row.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String link = (mItem instanceof Post) ? ((Post) mItem).Link : ((Product) mItem).Link;
				if (link == null) {
					Toast.makeText(getContext(), R.string.address_is_not_valid, Toast.LENGTH_SHORT).show();
					return;
				}
				link = (link.startsWith("http://") || link.startsWith("https://")) ? link : ("http://" + link);
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
				getContext().startActivity(intent);
			}
		});
		
		return row;
	}
	
	static class ViewHolder {
		ImageView mImage;
		TextView mTxtName, mTxtPrice;
	}
}
